import {takeEvery,call,put} from 'redux-saga/effects'
import api from '../http/api'
import {shopcartListAction} from '../redux/action'
function * getShopCartList(){
    //call函数saga提供用户用于网络请求的方法，第1个参数是网络api的方法名
    const result=yield call(api.shopcartList.getShopCartListApi)
    /*
      saga中获取到后端的数据后，要将数据给保存到store仓库中，必须发送通知，这里不能使用dispatch来发送通知
      saga中提供了一个用于向store仓库发送通知方法，该方法叫put
    */
   yield put(shopcartListAction(result.data.data))
}

function * changeNum({_id,n}){
  const result=yield call(api.shopcartList.changeNumApi,{_id,n})
  if(result.data.code){
    yield put({type:'getShopCartListAsync'})
  }
}

function * checkedItem({_id}){
  const result=yield call(api.shopcartList.checkedItemApi,_id)
  if(result.data.code){
    yield put({type:'getShopCartListAsync'})
  }
}

function * index(){
    yield takeEvery('getShopCartListAsync',getShopCartList)
    yield takeEvery('changeNumAsync',changeNum)
    yield takeEvery('checkedItemAsync',checkedItem)
}

export default index